package com.ruoyi.crop.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.crop.api.domain.Crop;
import com.ruoyi.crop.api.domain.Sprocess;
import com.ruoyi.crop.service.CropProcessService;
import com.ruoyi.crop.service.CropService;
import com.ruoyi.crop.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/process")
public class ProcessController extends BaseController {

    @Autowired
    private ProcessService processService;
    @Autowired
    private CropService cropService;

    //加工列表
    @GetMapping("/list")
    public TableDataInfo list(Sprocess process){
        startPage();
        LambdaQueryWrapper<Sprocess> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(process.getQuest()),Sprocess::getQuest,process.getQuest());
        List<Sprocess> list = processService.list(wrapper);
        return getDataTable(list);
    }


    //加工添加
    @PostMapping
    public AjaxResult saveMaterialLot(@RequestBody Sprocess process){
        process.setCreateTime(new Date());
        System.err.println(JSONUtil.toJsonStr(process));
        boolean save = processService.saves(process);
        return toAjax(save);
    }


    //加工列表修改
    @PutMapping
    public AjaxResult updateMaterialLot(@RequestBody Sprocess process){
        boolean b = processService.updateById(process);
        return toAjax(b);
    }


    //加工列表删除
    @DeleteMapping("/del")
    public AjaxResult delMaterialLot(Integer ids[]){
        boolean b = processService.removeBatchByIds(Arrays.asList(ids));
        return toAjax(b);
    }

    //根据id 获取 对象信息
    @RequestMapping("/getById/{processId}")
    public AjaxResult getById(@PathVariable Integer processId){
        Sprocess sprocess = processService.selectByProcessId(processId);
        System.err.println(JSONUtil.toJsonStr(sprocess));
        return success(sprocess);
    }

    //导出
    @Log(title = "生成加工列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<Sprocess> list = processService.list();
        ExcelUtil<Sprocess> util = new ExcelUtil<Sprocess>(Sprocess.class);
        util.exportExcel(response, list, "加工列表");
    }


    //农作物列表
    @RequestMapping("crop")
    public AjaxResult list(){
        List<Crop> list = cropService.list();
        return success(list);
    }

}
